package com.elhakimz.matra.scene;

import com.jme.system.canvas.SimpleCanvasImpl;
import com.jme.system.DisplaySystem;
import com.jme.system.lwjgl.LWJGLSystemProvider;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.math.FastMath;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.shape.Torus;
import com.jme.scene.Node;
import com.jme.scene.VBOInfo;
import com.jme.scene.state.FogState;
import com.jme.input.*;
import com.jme.input.action.KeyStrafeDownAction;
import com.jme.input.action.KeyStrafeUpAction;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.renderer.Renderer;
import com.jme.renderer.ColorRGBA;
import com.jme.light.DirectionalLight;
import com.jme.light.PointLight;
import com.jme.light.SimpleLightNode;
import com.jme.animation.SpatialTransformer;
import com.jmex.effects.water.ProjectedGrid;
import com.jmex.effects.transients.Fader;
import com.elhakimz.matra.util.GlobalVars;
import com.elhakimz.matra.util.SceneUtil;
import com.elhakimz.matra.actor.Actor;

import java.util.logging.Logger;

import com.elhakimz.matra.ui.InputManager;

/**
 * Created by IntelliJ IDEA.
 * User: abiel
 * Date: Feb 15, 2010
 * Time: 4:52:10 AM
 * To change this template use File | Settings | File Templates.
 */
class SceneImpl extends SimpleCanvasImpl {
    Fader fader;
    ProjectedGrid projectedGrid;
    private Vector3f axis;
    private Box box;
    private Sphere s;
    long startTime = 0;
    long fps = 0;
    private InputHandler input;

    private DisplaySystem display;

    InputManager inputManager = new InputManager();
    static final Logger logger = Logger.getLogger(SceneImpl.class.getName());

    public SceneImpl(int width, int height) {
        super(width, height);

    }

    @Override
    public void simpleSetup() {


        display = DisplaySystem
                .getDisplaySystem(LWJGLSystemProvider.LWJGL_SYSTEM_IDENTIFIER);
        //createInput();

        MouseInput.get().setCursorVisible(true);

        cam.setLocation(new Vector3f(100, 100, 200));
        cam.update();
        inputManager.setup(cam);
        inputManager.getOrbit().setWheelDirection(true);

        // The target position is the point the camera orbits around
        inputManager.getOrbit().setTargetPosition(new Vector3f(), true);
        input = inputManager;
                

        GlobalVars.pickNode = new Node();
        rootNode.attachChild(GlobalVars.pickNode);

        // Set the node used when picking
        inputManager.getCameraFocus().setNode(GlobalVars.pickNode);

        axis = new Vector3f(1, 1, 0.5f);
        axis.normalizeLocal();


        startTime = System.currentTimeMillis() + 5000;
        rootNode.updateRenderState();

        //setupLights();
        //setupObjects();
        //setupScene();
        setupFog();


        Actor act2 = new Actor("blue-2","blue","resources/matra/model/boat.3ds");
        act2.activate().setPos(100,10,0);

        Actor act1 = new Actor("blue-3","blue","resources/matra/model/boat.3ds");
        act1.activate().setPos(0,0,0);


        rootNode.attachChild(GlobalVars.sceneNode);
        rootNode.attachChild(GlobalVars.tracksNode);

    }


    public void simpleUpdate() {
        rootNode.updateGeometricState(0, false);
        rootNode.updateWorldBound();
        rootNode.updateRenderState();
        InputSystem.update();
        if (input != null)
            input.update(tpf);


    }

    MouseLookHandler mouseLook;
    KeyboardLookHandler keyboardLook;
    int camSpeed = 100;

    public void createInput() {
        input = new InputHandler();
        input.addAction(new InputAction() {
            public void performAction(InputActionEvent evt) {
                logger.info(evt.getTriggerName());
            }
        }, InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_ALL,
                InputHandler.AXIS_NONE, false);


        input.addAction(new InputAction() {
            public void performAction(InputActionEvent evt) {
                logger.info(evt.getTriggerName());
            }
        }, InputHandler.DEVICE_KEYBOARD, InputHandler.BUTTON_ALL,
                InputHandler.AXIS_NONE, false);


        cam = getCamera();

        // weise dem FirstPersonHandler die cam zu
        input = new FirstPersonHandler(cam, camSpeed, 1);


        // speichere MouseLook- und KeyboardLookHandler zuerst global
        mouseLook = (MouseLookHandler) input.getFromAttachedHandlers(0);
        keyboardLook = (KeyboardLookHandler) input.getFromAttachedHandlers(1);


        KeyBindingManager kmanager = KeyBindingManager.getKeyBindingManager();
        kmanager.add("Strafe Down", KeyInput.KEY_C);
        kmanager.add("Strafe Up", KeyInput.KEY_SPACE);

        // fuege neue Aktionen hinzu
        keyboardLook.addAction(new KeyStrafeDownAction(cam, camSpeed), "Strafe Down", true);
        keyboardLook.addAction(new KeyStrafeUpAction(cam, camSpeed), "Strafe Up", true);
        keyboardLook.setActionSpeed(camSpeed);

        input.removeAllFromAttachedHandlers();// entferne alle vorhandenen Handler vom input

        input.addToAttachedHandlers(keyboardLook);
        input.addToAttachedHandlers(mouseLook);
        InputAction buttonAction = new InputAction() {

            boolean mouselookAttached = true;

            public void performAction(InputActionEvent inputActionEvent) {


            }
        };

        input.addAction(buttonAction, InputHandler.DEVICE_MOUSE, InputHandler.BUTTON_ALL, InputHandler.AXIS_NONE, false);
        input.addAction(buttonAction, InputHandler.DEVICE_MOUSE, 1, InputHandler.AXIS_NONE, true);
        input.addAction(buttonAction, InputHandler.DEVICE_MOUSE, 2, InputHandler.AXIS_NONE, true);

    }





    private void setupFog() {
        FogState fogState = display.getRenderer().createFogState();
        fogState.setDensity(1.0f);
        fogState.setEnabled(true);
        fogState.setColor(new ColorRGBA(0.0f, 0.0f, 0.0f, 1.0f));
        fogState.setEnd(1000);
        fogState.setStart(0);
        fogState.setDensityFunction(FogState.DensityFunction.Linear);
        fogState.setQuality(FogState.Quality.PerVertex);
        rootNode.setRenderState(fogState);
    }


    /*@Override
    public void simpleUpdate() {
        input.update(tpf);

        // Code for rotating the box... no surprises here.
        if (tpf < 1) {
            angle = angle + (tpf * 25);
            if (angle > 360) {
                angle = 0;
            }
        }
        rotQuat.fromAngleNormalAxis(angle * FastMath.DEG_TO_RAD, axis);
        //box.setLocalRotation(rotQuat);
        //s.setLocalRotation(rotQuat);

        if (startTime > System.currentTimeMillis()) {
            fps++;
        } else {
            long timeUsed = 5000 + (startTime - System.currentTimeMillis());
            startTime = System.currentTimeMillis() + 5000;
            fps = 0;
        }
    }*/


}
